home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Software Vault: The Gold Collection
/
Software Vault - The Gold Collection (American Databankers) (1993).ISO
/
cdr50
/
picalc06.zip
/
PICALC.DOC
< prev
next >
Wrap
Text File
|
1993-06-08
|
4KB
|
106 lines
Program : PICALC
Version : 0.5
Function : Computes the value of PI from QuickBasic's Random Number
Generator.
Usage : From the DOS Prompt, enter "PICALC".
There are no command line options, Program in entirely menu
driven.
MainMenu : Allows the user to enter the options for the run,
Such as: Whether Automatic Samples are to be taken and if so
by what technique.
If Automatic Samples are to be taken - at what frequency.
If Samples are to be taken to the printer as well as to
the screen.
NOTE: The main menu is altered dynamically depending on what
options are chosen (ex: If no samples are to be taken,
the items relating to frequency and printer trace are
omitted.
Each selection from the main menu leads to a data input screen
for that selection. On completion of the data input, the program
then re-presents the main menu with the updated options.
RunTime : During the computation run, the user has the option of taking a
sample on demand (to the screen only), or taking a final sample
and terminating the run.
EndingRun : The program will loop through the main compute loop for
2,147,483,647 iterations or until the user gets sick of it and
hits the <ESCAPE> key to terminate it.
(On a 286 at 12.5 megahertz, the maximum iterations would take
somewhere around 75 days).
How? : Consider a circle with a radius of 1 unit centered on the origin
in the X-Y plane.
Now lets consider a square 2 units on a side around the circle.
We know the area of the square is 4 square units, and the area
of the circle is PI*R^2, or in this case PI since R = 1.
We also know that the equation for a circle centered at the
origin is R^2 = X^2 + Y^2, and also that the distance from the
origin to the circle itself is R = (X^2 + Y^2)^0.5.
Now to simplify things lets cut our figure into quarters and
only look at one of the quarters. We have a unit square with
an area of 1 square unit with a quarter circle in it with an
area of PI/4.
OK? - Now to the Random stuff.
We take our figure and
Pin it on the wall and throw darts at it, or
Lay it out in the backyard and let the rain fall on it, or
Fence off a square mile of meadowland and let my cow
(Pythagoras) drop cowpies on it, or
Generate X and Y values for a point on the figure.
When we finish, some of the items will be within the arc of the
circle, and some outside of it.
And all we have to do is count the number of items that fell
within the arc, divide this figure by the total number of items,
and multiply by 4 to get the value of PI.
Using Random Numbers, if the computed R is less than 1 then it is
within the arc. If greater than 1 then it is outside. Otherwise
we will toss a coin to see where to count it.
Why??? : Because it is do'able, and it gives the computer something to do
while it is resting.
Notes : Do NOT expect repeatable results. The random number generator is
primed by the value in the system timer.
Since Random Numbers are used in the computations, the program
does NOT converge to a value. (Wanders all over hell, but slowly
refines the value of PI).
Rather underwhelmed by QuickBasic's Random Number Generator, only
single precision numbers. PICALC 'fuzzes' them on conversions to
double precision. At a later date, I may incorporate my own
generator.
Program was first written about 1963 on an IBM-1620. Has been
through many machines and languages since then.
By : Tom Taylor
50 Bret Ave
San Rafael, CA 94901
Ph (415) 457-1071